<?php
namespace app\models;

use yii\db\ActiveRecord;
use yii\db\Expression;

/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/8/19
 * Time: 14:48
 */
class User_user extends ActiveRecord{

    public function getUserLists($limit, $offset, $cityCode, $startTime, $endTime, $userName, $userType, $userSort) {
        $user = User_user::find()->where(['is_deleted'=>0]);
        if ($cityCode != -1) {$user->andWhere(['city_code'=>$cityCode]);}

        if ($startTime != -1) {
            $user->andWhere(['between', 'gmt_create', $startTime, $endTime]);
        }

        if ($userName != -1){$user->andWhere(['like', 'user_nickname', '%'.$userName.'%', false]);}

        if ($userType != -1) {$user->andWhere(['like', 'forbidden_tag', $userType.'%', false]);}

        if ($userSort == 2) {
            $order = 'gmt_modified DESC';
        } else {
            $order = 'gmt_create DESC';
        }

        $user->andWhere(['not like', 'mobile', '168168']);

        $count = $user->count();
        $result = $user->orderBy($order)->limit($limit)->offset($offset)->asArray()->all();

        return ['total'=>$count, 'rows'=>$result];
    }


    public function confirmDayGift($user_id,$token,$type,$current_week){
        $user = User_user::find()->where([
            'del_flag'=>0,
            'user_id'=>$user_id,
            'token'=>$token
        ])->asArray()->one();

        if($type == 0){
            if($current_week == $user['threeDaysCheckWeek']){
                return $user;
            }else{
                return null;
            }
        }else{
            if($current_week == $user['sevenDaysCheckWeek']){
                return $user;
            }else{
                return null;
            }
        }
    }

    public function updateUserDayGift($user_id,$type,$daygift){
        if($type == 0){
            $result=\Yii::$app->db->createCommand()->update('user_user', [
                'threeDaysCheckWeek' => $daygift], ['user_id'=>$user_id])->execute();
        }else{
            $result=\Yii::$app->db->createCommand()->update('user_user', [
                'sevenDaysCheckWeek' => $daygift], ['user_id'=>$user_id])->execute();
        }

        return $result;
    }

    public function confirmUserByToken($user_id,$token){
        $user = User_user::find()->where([
            'del_flag'=>0,
            'user_id'=>$user_id,
            'token'=>$token
        ])->asArray()->one();

        if($user)
            return $user;
        else
            return null;

    }

    public function getUserById($user_id){
        $user = User_user::find()->where([
            'del_flag'=>0,
            'user_id'=>$user_id,
        ])->asArray()->one();

        return $user;
    }

    public function getUserByMobile($mobile){
        $user = User_user::find()->where([
            'del_flag'=>0,
            'mobile'=>$mobile,
        ])->asArray()->one();

        return $user;
    }

    public function getUserByHotcityNum($hotcity_num){
        $user = User_user::find()->select('user_id')->where([
            'del_flag'=>0,
            'hotcity_num'=>$hotcity_num,
        ])->asArray()->one();

        return $user;
    }

    public function getTotalAsset(){
        $sum = User_user::find()->where([
            'del_flag'=>0
        ])->sum('asset_diamond');
        return $sum;
    }

    public function getTotalIntegral(){
        $integral = User_user::find()->where([
            'del_flag'=>0
        ])->sum('integral');
        return $integral;
    }

    public function getTotalPay(){
        $pay = User_user::find()->where([
            'del_flag'=>0
        ])->sum('total_pay');
        return $pay;
    }

    public function getUserCount(){
        $count = User_user::find()->where([
            'del_flag'=>0
        ])->andWhere(['<>','type','2'])->count();
        return $count;
    }

    //是否为测试员
    public function getUsersByType($type,$limit,$offset,$mobile,$start,$end){
        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        if($type != -1){
            $post->andWhere(['type'=>$type]);
        }

        if($mobile != -1){
            if(strlen($mobile) == 11)
                $post->andWhere(['mobile'=>$mobile]);
            else
                $post->andWhere(['hotcity_num'=>$mobile]);
        }

        if($start != -1){
            $post->andWhere(['between','CREATED_DATE',$start,$end]);
        }

        $total = $post->count();
        $users = $post->orderBy('CREATED_DATE DESC')->limit($limit)->offset($offset)->asArray()->all();

        $result = ['total'=>$total,'rows'=>$users];
        return $result;
    }

    //根据会员等级统计用户
    public function getUsersByMember($start,$end){
        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        $post->andWhere(['between','integral',$start,$end]);

        $membersCount = $post->count();

        $result = ['people_count'=>$membersCount];

        return $result;
    }

    //根据性别查找用户
    public function getUsersBySex($sex,$limit,$offset){

        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        if($sex != -1){
            $post->andWhere(['sex'=>$sex]);
        }

        $total = $post->count();
        $users = $post->orderBy('CREATED_DATE DESC')->limit($limit)->offset($offset)->asArray()->all();

        $result = ['total'=>$total,'rows'=>$users];
        return $result;
    }

    //获取用户信息展示余额
    public function getUserForAsset($limit,$offset,$hotcity_num,$mobile){

        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        if($hotcity_num != -1){
            $post->andWhere(['hotcity_num' => $hotcity_num]);
        }

        if($mobile != -1){
            $post->andWhere(['mobile' => $mobile]);
        }

        $total = $post->count();
        $users = $post->orderBy('CREATED_DATE DESC')->limit($limit)->offset($offset)->asArray()->all();
        $asset = $post->sum('asset_diamond');

        $result = ['total'=>$total,'rows'=>$users,'money'=>$asset];
        return $result;
    }

    //获取土豪女神榜
    public function getUserRank($type,$limit,$offset,$user){

        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        if($user){
            $post->andWhere(['user_id' => $user['user_id']]);
        }

        $total = $post->count();
        if($type == 0){
            $users = $post->orderBy('consume_diamond DESC')->limit($limit)->offset($offset)->asArray()->all();
        }else{
            $users = $post->orderBy('hot_tickets DESC')->limit($limit)->offset($offset)->asArray()->all();

        }

        $result = ['total'=>$total,'rows'=>$users];
        return $result;
    }

    //根据手机查找用户
    public function getUsersByMobile($mobile,$limit,$offset){

        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        if($mobile != -1){
            if(strlen($mobile) == 11)
                $post->andWhere(['mobile'=>$mobile]);
            else
                $post->andWhere(['hotcity_num'=>$mobile]);
        }

        $total = $post->count();
        $users = $post->orderBy('CREATED_DATE DESC')->limit($limit)->offset($offset)->asArray()->all();

        $result = ['total'=>$total,'rows'=>$users];
        return $result;
    }

    //修改权重值
    public function editWeight($user_id,$weight)
    {
        $user = User_user::findOne($user_id);
        $user->weight_value = $weight;

        return $user->save();
    }

    public function getUserForFifty($sex,$limit,$offset){

        $post = User_user::find()->where([
            'del_flag' => 0,
            'sex'=>$sex
        ])->andWhere(['<>','type','2']);

        $total = $post->count();

        $post->orderBy(new Expression('rand()'));
        $allUsers = $post->asArray()->all();
        $users = $post->limit($limit)->offset($offset)->asArray()->all();

        $mobiles = array();
        for($index = 0;$index<count($allUsers);$index++){
            $mobile=array('mobile'=>$allUsers[$index]['mobile']);
            array_push($mobiles,$mobile);
            if($index >= 49){
                break;
            }
        }

        if($total > 50){
            $total = 50;
        }
        $result = ['total'=>$total,'rows'=>$users,'mobiles'=>$mobiles];
        return $result;
    }

    public function getAllMobile($mobile,$sex ,$start,$end){
        $post = User_user::find()->select('mobile')->where([
            'del_flag' => 0,
        ])->andWhere(['<>','type','2']);

        if($mobile != -1){
            $post->andWhere(['mobile'=>$mobile]);
        }

        if($sex != -1){
            $post->andWhere(['sex'=>$sex]);
        }

        if($start != -1)
            $post->andWhere(['between','UPDATED_DATE',$start,$end]);

        $mobiles =  $post->asArray()->all();
        return $mobiles;
    }

    public function updateUserTime($user_id,$time){
        $result=\Yii::$app->db->createCommand()->update('user_user', [
            'jqueryrotate_count' => $time], ['user_id'=>$user_id])->execute();

        return $result;
    }

    public function addUserTime($user_id){
        $post = User_user::findOne($user_id);
        $result = $post->updateCounters(['jqueryrotate_count' => 1]);
        return $result;
    }

    public function updateDiamondAsset($user_id,$diamond){

        User_user::findOne($user_id)->updateCounters(['asset_diamond' => $diamond]);
        $post = User_user::findOne($user_id);

        return $post->asset_diamond;
    }

    public function getUsersByUpdateTime($limit,$offset,$start,$end){
        $post = User_user::find()->where([
            'del_flag' => 0
        ])->andWhere(['<>','type','2']);

        if($start != -1){
            $post->andWhere(['between','UPDATED_DATE',$start,$end]);
        }

        $total = $post->count();
        $users = $post->orderBy('CREATED_DATE DESC')->limit($limit)->offset($offset)->asArray()->all();

        $result = ['total'=>$total,'rows'=>$users];
        return $result;
    }

    //获取马甲用户
    public function getVestUser($limit, $offset, $userStatus, $nickname, $cityCode) {
        $post = User_user::find()->joinWith(['sort']);

        $post->andWhere(['like','mobile',"168168"]);

        if ($cityCode != -1) {
            $post->andWhere(['city_code' => $cityCode]);
        }
        if ($userStatus != -1) {
            $post->andWhere(['is_deleted' => $userStatus]);
        }
        if ($nickname != -1) {
            $post->andWhere(['like','user_user.user_nickname',$nickname]);
        }

        $total = $post->count();
        $rows = $post->orderBy('user_id DESC')->limit($limit)->offset($offset)->asArray()->all();
        return ['total'=>$total,'rows'=>$rows];
    }

    //关联用户表 获取用户信息
    public function getSort()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_sort::className(), ['sort_id' => 'sort_id']);
    }

//    修改马甲状态
    public function editVestUser($userId, $userType) {
        $post = User_user::findOne(['user_id' => $userId]);
        $post->is_deleted = $userType;
        return $post->save();
    }

    public function updateVestData($user_id,$user_nickname,$photos,$sign){
        $user = User_user::findOne($user_id);
        $user->user_nickname = $user_nickname;

        if($photos != -1)
            $user->user_photos = $photos;

        if($sign != -1)
            $user->user_sign = $sign;

        return $user->save();
    }

//  修改马甲状态
        /*
         * 获取信誉积分排行榜
         */

    public function getReputationUserlist($limit, $offset, $sex, $mobile) {
        $post = User_user::find()->where(['user_user.del_flag'=>0])->select(['nickname','mobile','hotcity_num','sex','reputation_value']);
        if ($mobile != -1) {
            $post->andWhere(['user_user.mobile'=>$mobile]);
        } else {
            if ($sex != -1) {
                $post->andWhere(['user_user.sex'=>$sex]);
            }
        }
        $total = $post->count();
        $users = $post->orderBy('user_user.reputation_value DESC')->limit($limit)->offset($offset)->asArray()->all();
        return['total'=>$total, 'rows'=>$users];
    }

    public function editStatus($userId, $forbiden){
        $data = User_user::updateAll(['forbidden_tag' => $forbiden], ['user_id' => $userId]);

        if ($data) {
            return $data;
        }else{
            return -1;
        }
    }

//    修改马甲归属
    public function editAscription($userId, $adminId) {
        $user = User_user::findOne($userId);
        $user->admin_id = $adminId;

        return $user->save();
    }

    // 补全马甲信息
    public function addVest($userId, $sortId, $adminId, $userSign) {
        $user = User_user::findOne($userId);
        $user->admin_id = $adminId;
        $user->sort_id = $sortId;
        $user->user_sign = $userSign;

        return $user->save();
    }
}